Option Strict Off
Option Explicit On
Module MediaDir
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	'
	'  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
	'
	'  File:       media.bas
	'
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	
	'Registry constants
	Private Const KEY_READ As Integer = 131097
	Private Const REG_SZ As Short = 1
	Private Const HKEY_LOCAL_MACHINE As Integer = &H80000002
	'Registry API's
	Private Declare Function RegConnectRegistry Lib "advapi32.dll"  Alias "RegConnectRegistryA"(ByVal lpMachineName As String, ByVal hKey As Integer, ByRef phkResult As Integer) As Integer
	Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Integer) As Integer
	Private Declare Function RegQueryValueEx Lib "advapi32.dll"  Alias "RegQueryValueExA"(ByVal hKey As Integer, ByVal lpValueName As String, ByVal lpReserved As Integer, ByRef lpType As Integer, ByVal lpData As String, ByRef lpcbData As Integer) As Integer
	Private Declare Function RegOpenKeyEx Lib "advapi32.dll"  Alias "RegOpenKeyExA"(ByVal hKey As Integer, ByVal lpSubKey As String, ByVal ulOptions As Integer, ByVal samDesired As Integer, ByRef phkResult As Integer) As Integer
	
	Public Function FindMediaDir(ByVal sFile As String, Optional ByVal fUseCMedia As Boolean = False) As String
		If Dir(sFile, FileAttribute.Normal) <> vbNullString Then 'This file is the current folder
			FindMediaDir = AddDirSep(CurDir())
			Exit Function
		End If
		If fUseCMedia Then
			FindMediaDir = AddDirSep(GetDXSampleFolder) & "media\"
		Else
			FindMediaDir = AddDirSep(GetDXSampleFolder) & "vbsamples\media\"
		End If
	End Function
	
	Public Function AddDirSep(ByVal sPath As String) As String
		AddDirSep = sPath
		If Right(sPath, 1) <> "\" Then
			AddDirSep = sPath & "\"
		End If
	End Function
	
	Public Function GetDXSampleFolder() As String
		Dim lHandle As Integer
		Dim lNewHandle As Integer
		Dim sValue As String
		Dim lNewKey As Integer
		
		RegConnectRegistry(vbNullString, HKEY_LOCAL_MACHINE, lHandle)
		RegOpenKeyEx(lHandle, "SOFTWARE\Microsoft\DirectX", 0, KEY_READ, lNewHandle)
		sValue = Space(255)
		RegQueryValueEx(lNewHandle, "DX8SDK Samples Path", 0, REG_SZ, sValue, 255)
		If sValue <> Space(255) Then
			sValue = Left(sValue, InStr(sValue, Chr(0)) - 1)
		Else
			sValue = vbNullString
		End If
		RegCloseKey(lNewHandle)
		RegCloseKey(lHandle)
		GetDXSampleFolder = sValue
	End Function
End Module